<?php

namespace mpend\modules\shop\models;

use Yii;

/**
 * 购物车商品列表
 * 
 * @author emhome<emhome@163.com>
 * @since 2.0
 */
class CartSearch extends \common\models\shop\ShopCart {

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['user_id'], 'integer'],
        ];
    }

    /**
     * 检索购物车中商品
     * @return array
     */
    public function search() {
        $query = static::findActive('t')->andWhere([
            'and',
            ['>=', 't.status', static::STATUS_DEFAULT],
            ['t.user_id' => $this->user_id],
        ])
        ->orderBy(['t.created_at' => SORT_DESC]);
        $query->innerJoinWith(['product', 'goods']);
        return $query->all();
    }

    /**
     * @inheritdoc
     */
    public function fields() {
        return [
            'id',
            'name',
            'sku' => 'skuName',
            'price' => function() {
                return $this->goods ? floatval($this->goods->price) : 0;
            },
            'thumb' => function() {
                return $this->goods->thumb;
            },
            'stock',
            'product_id',
            'goods_id',
            'num',
            'checked',
            'status' => 'isValid',
            'invalid_tips' => 'invalidTips',
        ];
    }

}
